package com.example.exam.service.admin.Impl;

import com.example.exam.entity.User;
import com.example.exam.entity.UserRole;
import com.example.exam.entity.temp.TempStudent;
import com.example.exam.entity.temp.TempTeacher;
import com.example.exam.mapper.AdminMapper;
import com.example.exam.service.admin.AdminService;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author zhang
 */
@Service
@Log4j2
public class AdminServiceImpl implements AdminService {
    private final AdminMapper adminMapper;

    @Autowired
    public AdminServiceImpl(AdminMapper adminMapper) {
        this.adminMapper = adminMapper;
    }

    @Override
    public Integer create() {
//        UserRole userRole = new UserRole();
//        User user = new User();
//        String salt = RandomUtils.generateSalt();
//        user.setAccount(RandomUtils.getRandomString(8));
//        QueryWrapper<User> userWrapper = new QueryWrapper<>();
//        userWrapper.eq("account", user.getAccount());
//        int count = adminMapper.selectCount(userWrapper).intValue();
//        if (count > 0) {
//            logger.error("账号已存在");
//            return 0;
//        }
//        user.setId(0);
//        user.setName("admin");
//        user.setPassword(Md5Utils.toHex(Md5Utils.md5("123456" , salt)));
//        user.setTime(LocalDateTime.now());
//        user.setStatus(1);
//        user.setSex(1);
//        user.setAge(20);
//        user.setSalt(salt);
//        adminMapper.insert(user);
//        userRole.setUser_id(user.getId());
//        userRole.setRole_id(1);
//        adminMapper.insertRole(userRole);
        return 1;
    }

    @Override
    public List<TempTeacher> saveAllTeacher(List<TempTeacher> teachers) {
        List<User> users = new ArrayList<>();
        List<TempTeacher> failUser = new ArrayList<>();
        List<UserRole> roles = new ArrayList<>();
        //处理用户基本信息
        teachers.forEach(i -> {
            User user = new User();
            BeanUtils.copyProperties(i, user);
            if (user.getIdcard().isEmpty() || user.getName().isEmpty()
                    || user.getAge() == null || user.getSex() == null) {
                failUser.add(i);
                return;
            }
            user.userInit();
            log.info("用户信息:{}", user);
            users.add(user);
        });
        //保存用户信息
        adminMapper.saveAllTeacher(users);

        //处理用户角色
        users.forEach(i -> {
            UserRole role = new UserRole();
            role.setUserId(i.getId());
            role.setRoleId(UserRole.TEACHER);
        });
        //保存用户角色
        adminMapper.saveAllUserRole(roles);
        return failUser;
    }

    @Override
    public List<TempStudent> saveAllStudent(List<TempStudent> students) {
        return Collections.emptyList();
    }
}
